package com.esl.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.esl.dao.CommonDao;
import com.esl.dto.admin.PageInfo_Dto;
import com.googlecode.ehcache.annotations.Cacheable;

public class CommonDaoImpl<T extends Serializable> implements CommonDao<T> {

	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	/*------------------------------------------------------------------*/
	public T save(T entity) {
		hibernateTemplate.save(entity);
		return entity;
	}

	public boolean deleteById(Class<T> clazz, Serializable id) {
		Object object = hibernateTemplate.get(clazz, id);
		if (object != null) {
			hibernateTemplate.delete(object);
			return true;
		}
		return false;
	}

	@Transactional(readOnly = true)
	public <T> T findById(Class<T> clazz, Serializable id) {
		return (T) hibernateTemplate.get(clazz, id);
	}

	@Cacheable(cacheName = "pagecache")
	public PageInfo_Dto findByPageInfo(Class<T> clazz, Integer pageNow,
			Integer pageSize, String hql, String orderby) {
		String quaryPageItemsHql = null;
		Integer totalPage = 0;
		if (hql.equals("") & orderby.equals("")) {
			quaryPageItemsHql = "from " + clazz.getName();
		} else if (!hql.equals("") & orderby.equals("")) {
			quaryPageItemsHql = "from " + clazz.getName() + hql;
		} else if (hql.equals("") & !orderby.equals("")) {
			quaryPageItemsHql = "from " + clazz.getName() + orderby;
		} else {
			quaryPageItemsHql = "from " + clazz.getName() + hql + orderby;
		}
		int total = hibernateTemplate.find(quaryPageItemsHql).size();
		List datas = hibernateTemplate.getSessionFactory().openSession()
				.createQuery(quaryPageItemsHql)
				.setFirstResult(pageSize * (pageNow - 1))
				.setMaxResults(pageSize).list();
		if (total % pageSize == 0) {
			totalPage = total / pageSize;
		} else {
			totalPage = total / pageSize + 1;
		}
		PageInfo_Dto pm = new PageInfo_Dto();
		pm.setPagesize(pageSize);
		pm.setTotalPage(totalPage);
		pm.setPageNow(pageNow);
		pm.setData(datas);
		return pm;
	}

	/*------------------------------------------------------------------*/

	public List<T> findByHql(String hql) {
		List<T> ts = hibernateTemplate.find(hql);
		if (ts == null) {
			return new ArrayList<T>();
		}
		return ts;
	}

	public List<T> findTopRecordByHql(String hql, Integer howMany) {
		List<T> ts = hibernateTemplate.getSessionFactory().getCurrentSession()
				.createQuery(hql).setMaxResults(howMany).list();
		if (ts == null) {
			return new ArrayList<T>();
		}
		return ts;
	}

	public void update(T entity) {
		hibernateTemplate.update(entity);
	}

	public T merge(T entity) {
		return (T) this.hibernateTemplate.merge(entity);
	}

	public void saveOrUpdate(T entity) {
		this.hibernateTemplate.saveOrUpdate(entity);
	}

	public void flush() {
		this.hibernateTemplate.flush();
	}

}
